<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
  <head>
    <title>RoleCall Provider</title>
    <meta name='viewport' content='width=320, user-scalable=yes' />
    <script type='text/javascript' language='javascript'>
    
    /** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
     ** Code licensed under Creative Commons Attribution-ShareAlike License      **
     ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
    function XHConn()
    {
      var xmlhttp, bComplete = false;
      try { xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); }
      catch (e) { try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); }
      catch (e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (e) { xmlhttp = false; }}}
      if (!xmlhttp) return null;
      this.connect = function(sURL, sMethod, sVars, fnDone) {
        if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
        try {
          if (sMethod == 'GET') {
            xmlhttp.open(sMethod, sURL+'?'+sVars, true);
            sVars = '';
          } else {
            xmlhttp.open(sMethod, sURL, true);
            xmlhttp.setRequestHeader('Method', 'POST '+sURL+' HTTP/1.1');
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          }
          xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && !bComplete) {
              bComplete = true;
              fnDone(xmlhttp);
            }};
          xmlhttp.send(sVars);
        }
        catch(z) { return false; }
        return true;
      };
      return this;
    }
    
    var ajaxConn = new XHConn();
    
    function getStatusCallback(statusStr){
      var status = eval('('+statusStr.responseText+')');
      var user = status.user;
      if( null == user || undefined == user ){
        document.cookie='openid_identifier=; path=/';
        var buttonnode= document.createElement('input');
        buttonnode.setAttribute('type','button');
        buttonnode.setAttribute('value','login');
        buttonnode.onclick = function() {ajaxConn.connect('listProviders','POST','',listProvidersCallback)};
        document.body.appendChild(buttonnode);
      }
      else
      {
        var welcomeDiv = document.createElement('div');
        welcomeDiv.innerHTML='<b>Welcome '+user.name+'</b> &lt;'+user.email+'&gt; ('+user.openId+')';
        document.body.appendChild(welcomeDiv);
        var rolesDiv = document.createElement('div');
        rolesDiv.innerHTML='<b>Roles:</b> '+user.roles;
        document.body.appendChild(rolesDiv);
        var form = document.createElement('form');
        form.action = document.URL;
        form.method = 'post';
        document.body.appendChild(form);
        var buttonnode= document.createElement('input');
        buttonnode.setAttribute('type','submit');
        buttonnode.setAttribute('value','logout');
        form.appendChild(buttonnode);
        var hidden = document.createElement('input');
        hidden.setAttribute('type','hidden');
        hidden.setAttribute('name','logout');
        hidden.setAttribute('value','true');
        form.appendChild(hidden);
      }
    }
    
    function listProvidersCallback(providersStr){
      var providers = eval('('+providersStr.responseText+')');
      while(document.body.hasChildNodes()){
          document.body.removeChild(document.body.childNodes[0]);
      }
      var divnode = document.createElement('div');
      divnode.innerHTML='Login via:';
      document.body.appendChild(divnode);
      for( var i = 0; i < providers.length; ++i ){
          var divnode = document.createElement('div');
          var buttonnode= document.createElement('button');
          buttonnode.onclick = loginOpenIdFunc(providers[i].url, providers[i].prompt);
          buttonnode.innerHTML='<img style="width:16px;height:16px" src="'
              + providers[i].faviconURL + '"/> <b>'
              + providers[i].name + '</b>'
          divnode.appendChild(buttonnode);
          document.body.appendChild(divnode);
      }
      divnode = document.createElement('div');
      var cbnode= document.createElement('input');
      cbnode.setAttribute('type','checkbox');
      cbnode.setAttribute('id','keepLoginBox')
      cbnode.setAttribute('checked','true')
      divnode.appendChild(cbnode);
      var spannode = document.createElement('span');
      spannode.innerHTML = 'Keep me logged in.';
      divnode.appendChild(spannode);
      document.body.appendChild(divnode);
    }
        
    function loginOpenIdFunc(openIdUrl, promptStr) {
        return function() {loginOpenId(openIdUrl, promptStr);};  
    }
        
    function loginOpenId(openIdUrl, promptStr) {
        if (null != promptStr && promptStr.length > 0) {
            promptResult = prompt(promptStr, '');
            if (null != promptResult) {
                openIdUrl = openIdUrl.replace('%s', promptResult);
            } else {
                return;
            }
        }

        var keepLoginBox = document.getElementById('keepLoginBox');
        if (null != keepLoginBox && keepLoginBox.checked) {
            var exdate=new Date();
            exdate.setDate(exdate.getDate() + 365);
            var c_value=escape(openIdUrl) + '; path=/; expires='+exdate.toUTCString() ;
            document.cookie='openid_identifier=' + c_value;
        }

        var form = document.createElement('form');
        form.method = 'post';
        form.action = document.URL;
        document.body.appendChild(form);
        var hidden = document.createElement('input');
        hidden.setAttribute('type','hidden');
        hidden.setAttribute('name','openid_identifier');
        hidden.setAttribute('value',openIdUrl);
        form.appendChild(hidden);
        form.submit();
    }
        
    function initialize()
    {
      ajaxConn.connect('getStatus','POST','',getStatusCallback);
    }
    
    </script>
  </head>
  <body onload='initialize();'>
  </body>
<html>
